﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaoSH.AppCode;
using System.Data;
using TaoSH.Data;
using Newtonsoft.Json;


namespace TaoSH.View
{
    public partial class CommonListN : BasePage
    {
        public string strFormCode
        {
            get { return Request.QueryString["FormCode"] ?? "XTGG"; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
            string strQueryName = "../../View/UCList/" + FormBaseData.FormQueryUrl;
            QueryUControl Control = new QueryUControl();
            if (!string.IsNullOrEmpty(FormBaseData.FormQueryUrl))
            {
                Control = LoadControl(strQueryName) as QueryUControl;
                Control.ID = "UserQueryControl";
                QueryCon.Controls.Add(Control);

                Button btQuery = Control.FindControl("btQuery") as Button;
                if (btQuery != null)
                {
                    btQuery.Click += new EventHandler(this.btQuery_Click);

                }

            }
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(FormBaseData.FormQueryUrl))
                {
                    querydata(Control.SetinitQuery() + Control.SetDefaultQuery());
                }
                else
                {
                    querydata("");
                }
            }
        }


        public void querydata(string strWhere)
        {
            string strReturn = "";

            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);

            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }
            DataTable dtResult = new DataTable();
            if (strFormCode == "JZBM")
            {
                dtResult = new JH_Auth_FormBaseB().GetDTByCommand(" select  * from  " + strFormTable + " where 1=1 " + strWhere + " ORDER BY ID  ");
            }
            else if (strFormCode == "DDCL")
            {

                string user = "";
                if (UserInfo.User.UserName != "admin")
                {
                    user = " and CreateUser='" + UserInfo.User.UserName + "'";
                }
                dtResult = new JH_Auth_FormBaseB().GetDTByCommand(" select  * from  " + strFormTable + " where 1=1 " + strWhere + user + " ORDER BY ID  ");
            }
            else
            {
                dtResult = new JH_Auth_FormBaseB().GetDTByCommand(" select  * from  " + strFormTable + " where 1=1 " + strWhere + " ORDER BY ID DESC ");

            }
            var timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };
            strReturn = JsonConvert.SerializeObject(dtResult, timeConverter);
            tabledata.Value = strReturn;
        }


        void btQuery_Click(Object sender, EventArgs e)
        {
            QueryUControl Control = QueryCon.FindControl("UserQueryControl") as QueryUControl;
            string strQuery = Control.SetinitQuery() + Control.GetQuerydata();
            hidselID.Value = "";//初始化选择为空
            querydata(strQuery);
        }
    }
}